﻿using System;
using System.Collections.Generic;
using UFrameWork;
using UnityEngine;

public class TestController : UController  
{
    protected static UNetClient Server;

    [UControllerAction("cmd.connect.server")]
    public static WaitForAction ConnectServer()
    {
        Server = new UNetClient("localhost", 2016);
        return new WaitForAction(UEvent.NET_CONNECT_SUCCESS);
    }

    [UControllerAction(UEvent.NET_CONNECT_SUCCESS)]
    public static void OnServerConnected(UNetClient server)
    {
        Debug.Log(string.Format("{0}:{1} Connected!",server.Host,server.Port));
        WaitForAction.End(UEvent.NET_CONNECT_SUCCESS);
    }

    [UControllerAction("cmd.send.hello")]
    public WaitForAction SendHello(string str)
    {
        SendMsgToServer("msg.test.hello",str);
        return new WaitForAction("msg.test.hello.resp");
    }

    [UControllerAction("msg.test.hello.resp")]
    public void OnMsgHelloResp(string str)
    {
        WaitForAction.End("msg.test.hello.resp",str);
    }

    [UControllerAction("cmd.send.rank")]
    public WaitForAction SendRank()
    {
        SendMsgToServer("msg.test.rank");
        return new WaitForAction("msg.test.rank.resp");
    }

    [UControllerAction("msg.test.rank.resp")]
    public void OnMsgRankResp(int self,List<string> list)
    {
        WaitForAction.End("msg.test.rank.resp", self,list);
    }

    /// <summary>
    /// 发送数据到主服务器
    /// </summary>
    protected void SendMsgToServer(string key, params object[] data)
    {
        Server.Send(key, data);
    }
}